import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { CaptchaResult, LoginData, LoginResult } from "./types";

/**
 * 登录API
 *
 * @param data {LoginData}
 * @returns
 */
export function loginApi(data: LoginData): AxiosPromise<LoginResult> {
  const formData = new FormData();
  const param = {
    username: data.username,
    password: data.password,
    usertype: data.usertype,
    code: data.code,
    id: data.id,
  };
  return request({
    url: "/api/v1/auth/login",
    method: "post",
    data: param,
  });
}

/**
 * 刷新token
 */
export function refreshTokenApi() {
  return request({
    url: "/api/v1/auth/refresh_token",
    method: "post",
  });
}

/**
 * 注销API
 */
export function logoutApi() {
  return request({
    url: "/api/v1/auth/logout",
    method: "delete",
  });
}

/**
 * 获取验证码
 */
export function getCaptchaApi(type = 0): AxiosPromise<CaptchaResult> {
  return request({
    url: "/api/v1/auth/captcha",
    method: "get",
    params: { codeType: type },
  });
}

/**
 * 校验用户名密码
 */
export function VerifyUserNameLogin(code: string, password: string) {
  return request({
    url: "/api/v1/auth/verifyusername_login/" + code,
    method: "get",
    params: {
      password: password,
    },
  });
}
/**
 * 校验用户名
 */
export function VerifyUserName(code: string) {
  return request({
    url: "/api/v1/auth/verifyusername/" + code,
    method: "get",
  });
}

/**
 * 获取重置密码token
 */
export function GetRetrievePasswordToken(userType: string, loginName: string, verifyCode: string) {
  return request({
    url: "/api/v1/account/getretrievepasswordtoken/" + userType,
    method: "get",
    params: {
      loginName: loginName,
      verifyCode: verifyCode,
    },
  });
}
/**
 * 手机获取验证码
 *
 * @param data
 */
export function GetVerificationcode(userName: string, userType: string) {
  return request({
    url: "/api/v1/auth/verificationcode/" + userName,
    method: "get",
    params: { userType },
  });
}

/**
 * 获取短信发送状态
 */
export function GetSMSSendStatus(token: string) {
  return request({
    url: "/api/v1/account/getSMSSendStatus/" + token,
    method: "get",
  });
}
/**
 * 提交重置密码
 */
export function SubmitRetrievePassword(token: string, passWord: string) {
  return request({
    url: "/api/v1/account/submitRetrievePassword/" + token,
    method: "put",
    params: {
      passWord: encryptSM3(passWord),
    },
  });
}
